//
//  MahjongLogoutView.m
//  Mahjong
//
//  Created by 饶首建 on 17/7/3.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongLogoutView.h"
#import "HWPopTool.h"
#import "QRCode.h"

#import "ResultModel.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface MahjongLogoutView()
@property (weak, nonatomic) IBOutlet UILabel *totalGameCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *avatarImageViewA;
@property (weak, nonatomic) IBOutlet UILabel *nicknameA;
@property (weak, nonatomic) IBOutlet UILabel *scoreA;
@property (weak, nonatomic) IBOutlet UILabel *firstLabelA;

@property (weak, nonatomic) IBOutlet UIImageView *avatarImageViewB;
@property (weak, nonatomic) IBOutlet UILabel *nicknameB;
@property (weak, nonatomic) IBOutlet UILabel *scoreB;
@property (weak, nonatomic) IBOutlet UILabel *firstLabelB;

@property (weak, nonatomic) IBOutlet UIImageView *avatarImageViewC;
@property (weak, nonatomic) IBOutlet UILabel *nicknameC;
@property (weak, nonatomic) IBOutlet UILabel *scoreC;
@property (weak, nonatomic) IBOutlet UILabel *firstLabelC;

@property (weak, nonatomic) IBOutlet UIImageView *avatarImageViewD;
@property (weak, nonatomic) IBOutlet UILabel *nicknameD;
@property (weak, nonatomic) IBOutlet UILabel *scoreD;
@property (weak, nonatomic) IBOutlet UILabel *firstLabelD;

@property (weak, nonatomic) IBOutlet UIImageView *QRCodeImageView;
@end

@implementation MahjongLogoutView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setGameModel:(GameModel *)gameModel{
    
    _avatarImageViewA.layer.cornerRadius = _avatarImageViewA.frame.size.width/2;
    _avatarImageViewB.layer.cornerRadius = _avatarImageViewB.frame.size.width/2;
    _avatarImageViewC.layer.cornerRadius = _avatarImageViewC.frame.size.width/2;
    _avatarImageViewD.layer.cornerRadius = _avatarImageViewD.frame.size.width/2;
    _avatarImageViewA.layer.masksToBounds = YES;
    _avatarImageViewB.layer.masksToBounds = YES;
    _avatarImageViewC.layer.masksToBounds = YES;
    _avatarImageViewD.layer.masksToBounds = YES;
    
    //总分
    NSMutableArray *tmp = [NSMutableArray arrayWithArray:_gameArr];
    
    NSInteger rscoreA = 0;
    NSInteger rscoreB = 0;
    NSInteger rscoreC = 0;
    NSInteger rscoreD = 0;
    //玩家A的dataSet
    PlayerModel *playerA = _matchModel.players.count>0?_matchModel.players[0]:nil;
    PlayerModel *playerB = _matchModel.players.count>1?_matchModel.players[1]:nil;
    PlayerModel *playerC = _matchModel.players.count>2?_matchModel.players[2]:nil;
    PlayerModel *playerD = _matchModel.players.count>3?_matchModel.players[3]:nil;
    for (int i=0; i< tmp.count; i++) {
        GameModel *gm = tmp[i];
        for (ResultModel *rm in gm.resultArr) {
            if ([rm.playerModel.playerId isEqualToString:playerA.playerId]) {
                rscoreA += rm.score;
            }else if ([rm.playerModel.playerId isEqualToString:playerB.playerId]){
                rscoreB += rm.score;
            }else if ([rm.playerModel.playerId isEqualToString:playerC.playerId]){
                rscoreC += rm.score;
            }else if ([rm.playerModel.playerId isEqualToString:playerD.playerId]){
                rscoreD += rm.score;
            }
        }
    }
    //playerA
    _nicknameA.text = playerA.playerName;
    if ([_owner.playerId isEqualToString:playerA.playerId]) {
        _nicknameA.textColor = UIColorHex(66B5F0);
    }
    [_avatarImageViewA sd_setImageWithURL:[NSURL URLWithString:playerA.avatar] placeholderImage:UIImageWithName(@"default_head")];
    if (playerA.avatar.length == 0) {
        _firstLabelA.text = [playerA.playerName substringToIndex:1];
    }else{
        _firstLabelA.text = @"";
    }
    if (rscoreA > 0) {
        _scoreA.textColor = UIColorHex(F15A5A);
        _scoreA.text = NSStringFormat(@"+%ld",rscoreA);
    }else if (rscoreA == 0){
        _scoreA.textColor = UIColorHex(9B9B9B);
        _scoreA.text = NSStringFormat(@"%ld",rscoreA);
    }else{
        _scoreA.textColor = UIColorHex(519B00);
        _scoreA.text = NSStringFormat(@"%ld",rscoreA);
    }
    //playerB
    _nicknameB.text = playerB.playerName;
    if ([_owner.playerId isEqualToString:playerB.playerId]) {
        _nicknameB.textColor = UIColorHex(66B5F0);
    }
    [_avatarImageViewB sd_setImageWithURL:[NSURL URLWithString:playerB.avatar] placeholderImage:UIImageWithName(@"default_head")];
    if (playerB.avatar.length == 0) {
        _firstLabelB.text = [playerB.playerName substringToIndex:1];
    }else{
        _firstLabelB.text = @"";
    }
    if (rscoreB > 0) {
        _scoreB.textColor = UIColorHex(F15A5A);
        _scoreB.text = NSStringFormat(@"+%ld",rscoreB);
    }else if (rscoreB == 0){
        _scoreB.textColor = UIColorHex(9B9B9B);
        _scoreB.text = NSStringFormat(@"%ld",rscoreB);
    }else{
        _scoreB.textColor = UIColorHex(519B00);
        _scoreB.text = NSStringFormat(@"%ld",rscoreB);
    }
    //playerC
    _nicknameC.text = playerC.playerName;
    if ([_owner.playerId isEqualToString:playerC.playerId]) {
        _nicknameC.textColor = UIColorHex(66B5F0);
    }
    [_avatarImageViewC sd_setImageWithURL:[NSURL URLWithString:playerC.avatar] placeholderImage:UIImageWithName(@"default_head")];
    if (playerC.avatar.length == 0) {
        _firstLabelC.text = [playerC.playerName substringToIndex:1];
    }else{
        _firstLabelC.text = @"";
    }
    if (rscoreC > 0) {
        _scoreC.textColor = UIColorHex(F15A5A);
        _scoreC.text = NSStringFormat(@"+%ld",rscoreC);
    }else if (rscoreC == 0){
        _scoreC.textColor = UIColorHex(9B9B9B);
        _scoreC.text = NSStringFormat(@"%ld",rscoreC);
    }else{
        _scoreC.textColor = UIColorHex(519B00);
        _scoreC.text = NSStringFormat(@"%ld",rscoreC);
    }
    //playerD
    _nicknameD.text = playerD.playerName;
    if ([_owner.playerId isEqualToString:playerD.playerId]) {
        _nicknameD.textColor = UIColorHex(66B5F0);
    }
    [_avatarImageViewD sd_setImageWithURL:[NSURL URLWithString:playerD.avatar] placeholderImage:UIImageWithName(@"default_head")];
    if (playerD.avatar.length == 0) {
        _firstLabelD.text = [playerD.playerName substringToIndex:1];
    }else{
        _firstLabelD.text = @"";
    }
    if (rscoreD > 0) {
        _scoreD.textColor = UIColorHex(F15A5A);
        _scoreD.text = NSStringFormat(@"+%ld",rscoreD);
    }else if (rscoreD == 0){
        _scoreD.textColor = UIColorHex(9B9B9B);
        _scoreD.text = NSStringFormat(@"%ld",rscoreD);
    }else{
        _scoreD.textColor = UIColorHex(519B00);
        _scoreD.text = NSStringFormat(@"%ld",rscoreD);
    }
    
    _totalGameCountLabel.text = NSStringFormat(@"%ld",_gameArr.count);
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    NSInteger delta = now - _matchModel.startDate.doubleValue/1000;
    NSInteger seconds = delta % 60;
    NSInteger minutes = (delta / 60) % 60;
    NSInteger hours = delta / 3600;
    _timeLabel.text = NSStringFormat(@"%ld小时%ld分%ld秒",(long)hours,(long)minutes,(long)seconds);
    
    NSString *url = NSStringFormat(@"%@/api/Matches/%@/result",WECHAT_QRCODE_URL,_matchModel.matchId);
    _QRCodeImageView.image = [QRCode qrCode:_QRCodeImageView.frame.size.width url:url];
}

- (void)awakeFromNib{
    [super awakeFromNib];
}

+(instancetype)mahjongLogoutView{
    return [[[NSBundle mainBundle] loadNibNamed:@"MahjongLogoutView"
                                          owner:nil options:nil]lastObject];
}

- (IBAction)logoutAction:(id)sender {
    if (_logoutTapBlock) {
        _logoutTapBlock();
    }
}

- (IBAction)cancelAction:(id)sender {
    [[HWPopTool sharedInstance] closeWithBlcok:^{}];
}

@end
